package com.hyts.standard.webflux.base.route;
import com.hyts.standard.webflux.base.handler.AbstractCrudDataHandler;
import com.hyts.standard.webflux.base.handler.AbstractViewPageHandler;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import java.util.Optional;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * @project-name:standard-webflux
 * @package-name:com.hyts.standard.webflux.route
 * @author:Libo/Alex
 * @create-date:2019-06-19 9:51
 * @email:liboware@gmail.com
 * @description:此类主要用于: 路由相关配置服务
 */
@NoArgsConstructor
public abstract class AbstractDataRoute<T extends AbstractCrudDataHandler,V extends AbstractViewPageHandler> extends
        AbstractPageRoute{

    @Setter
    private T datsHandler;

    /**
     * 执行操作服务
     * @return
     */
    public RouterFunction<ServerResponse> crudDataRoute(T datsHandler,V pageHandler,String prefixPath) {
        Optional.of(datsHandler);
        Optional.of(pageHandler);
        prefixPath = Optional.ofNullable(prefixPath).orElse("/");
        return route(GET(prefixPath+"/save"),datsHandler::save).
            andRoute(GET(prefixPath+"/remove"),datsHandler::remove).
            andRoute(GET(prefixPath+"/load"),datsHandler::load).
            andRoute(GET(prefixPath+"/search"),datsHandler::search).
                and(crudPageRoute(prefixPath,pageHandler));
    }


}
